package com.example.questionsystem;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

import java.awt.*;
import java.net.URI;

@MapperScan("com.example.questionsystem.modules.*.mapper")
@SpringBootApplication
public class QuestionSystemApplication {

	public static void main(String[] args) {
		System.setProperty("java.awt.headless", "false"); // 关闭 headless 模式
		SpringApplication.run(QuestionSystemApplication.class, args);

	}


	@EventListener(ApplicationReadyEvent.class)
	public void openBrowserAfterStartup() {
		try {
			// 确保后端已完全启动（可适当延迟）
			Thread.sleep(1000); // 延迟1秒避免端口未就绪

			// 使用系统默认浏览器打开URL
			if (Desktop.isDesktopSupported()) {
				Desktop desktop = Desktop.getDesktop();
				if (desktop.isSupported(Desktop.Action.BROWSE)) {
					desktop.browse(new URI("http://localhost:8101"));
				}
			} else {
				// 备用方案：调用Windows命令
				Runtime.getRuntime().exec("cmd /c start http://localhost:8101");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
